package com.lz.ssm.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.pagehelper.PageInfo;
import com.lz.ssm.domain.Department;
import com.lz.ssm.domain.Employee;
import com.lz.ssm.qo.EmployeeQueryObject;
import com.lz.ssm.qo.JsonResult;
import com.lz.ssm.qo.PageResult;
import com.lz.ssm.service.IDepartmentService;
import com.lz.ssm.service.IEmployeeService;
import com.lz.ssm.service.IRoleService;
import com.lz.ssm.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author zzz
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    /**
     * 查看所有员工信息
     * @param model 数据
     * @return 员工信息
     */
    @RequestMapping("/list")
    @RequiredPermission(name="员工浏览",expression = "employee:list")
    public String list(@ModelAttribute("qo") EmployeeQueryObject qo, Model model) {

        //分页 过滤
        //PageResult<Employee> employees = employeeService.query(qo);
        PageInfo<Employee> employees = employeeService.query(qo);
        model.addAttribute("result", employees);
        List<Department> departments = departmentService.listAll();
        model.addAttribute("depts",departments);
        return "employee/list";
    }

    /**
     * 删除一个员工信息
     * @param id 员工id
     * @return 员工信息页
     */
    @RequestMapping("/delete")
    @ResponseBody
    @RequiredPermission(name="员工删除",expression = "employee:delete")
    public JsonResult delete(Long id) {
        try {
            if (id != null) {
                employeeService.delete(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("删除失败");
        }

        return new JsonResult(true,"删除成功");
    }

    /**
     * 修改
     * @param id 员工id
     * @param model 数据
     * @return 修改页
     */
    @RequestMapping("/input")
    @RequiredPermission(name="员工编辑",expression = "employee:input")
    public String input(Long id, Model model) {
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        model.addAttribute("roles",roleService.listAll());
        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);
        }
        return "/employee/input";
    }
    /**
     * 新增或修改
     * @param employee 员工对象
     * @return 员工信息页
     */
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="员工更新",expression = "employee:saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] ids) {
        if (employee.getId() == null) {
            //新增
            //employeeService.save(employee);
            employeeService.save(employee, ids);
        } else {
            employeeService.update(employee,ids);
             //修改
        }
        // 让浏览器重新发一次请求  
        return "redirect:/employee/list";
    }

}